const shareUtil = require('../../../utils/shareUtil.js');
const app = getApp();

Page({
    data: {
        time: "22:42",
        sec: "13",
        height: (app.globalData.screenHeight - 220) / 2,
        width: app.globalData.screenHeight,
        diretion: "rotate(90deg)",
        flag: true,
        changeColor: 0,
        display: "",
        test: ""
    },
    onLoad: function(t) {},
    onReady: function() {},
    change: function() {
        0 == this.data.changeColor ? this.setData({
            changeColor: 1
        }) : 1 == this.data.changeColor && this.setData({
            changeColor: 0
        });
    },
    onShow: function() {
        this.displayTime(this), this.data.display = setInterval(this.displayTime, 1e3, this), 
        this.data.test = setInterval(this.testState, 500);
    },
    testState: function() {
        var t = this, e = Date.now();
        wx.startAccelerometer(), wx.onAccelerometerChange(function(a) {
            var n = Date.now();
            if (!(n - e < 500)) {
                e = n;
                var i = 57.3 * Math.atan2(-a.x, Math.sqrt(a.y * a.y + a.z * a.z));
                Math.atan2(a.y, a.z);
                if (console.log(i), i > -1 && i <= 180 || i > -360 && i < -180 || -0 == i) {
                    if (1 == t.data.flag) return;
                    0 == t.data.flag && t.setData({
                        diretion: "rotate(90deg)"
                    }), t.setData({
                        flag: true
                    });
                } else if (i < -1 && i >= -180 || i > 180 && i < 360) {
                    if (0 == t.data.flag) return;
                    1 == t.data.flag && t.setData({
                        diretion: "rotate(-90deg)"
                    }), t.setData({
                        flag: false
                    });
                }
            }
        });
    },
    displayTime: function(t) {
        var e, a = new Date(), n = a.getHours(), i = a.getMinutes(), s = a.getSeconds();
        n < 10 && (n = "0" + n), i < 10 && (i = "0" + i), s < 10 && (s = "0" + s), e = n + ":" + i, 
        t.time = e, t.sec = s, this.setData({
            time: e,
            sec: s
        });
    },
    onHide: function() {
        clearInterval(this.data.display), clearInterval(this.data.test), wx.stopAccelerometer();
    },
    onUnload: function() {
        clearInterval(this.data.display), clearInterval(this.data.test), wx.stopAccelerometer();
    },
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage:shareUtil.shareConfig,
    onShareTimeline:shareUtil.shareConfig,
});